﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using StockMatch.Service;
using StockMatch.Service.Entity;
using Titan;

namespace StockMatch.WebSite.Areas.Admin.Controllers
{
    /// <summary>
    /// 对于注册用户的管理
    /// </summary>
    public class UserController : AdminControllerBase
    {

        public ActionResult Index(int? page, string kw)
        {
            ViewBag.Menu = "User";
            ViewBag.Tag = "Index";

            int pageIndex = (page == null ? 1 : (int) page);

            #region 查询列表

            var query = new QueryExpression();
            query.EntityType = typeof (User);
            query.Select(User_.ALL);
            if (!string.IsNullOrEmpty(kw))
            {
                ConditionExpressionCollection ce = new ConditionExpressionCollection();
                ce.Add(User_.UserName.TLike(kw));
                ce.Add(User_.UserTrueName.TLike(kw));
                ce.ConditionRelation = ConditionRelation.Or;
                query.Wheres.Add(ce);
            }
            query.OrderBys.Add(User_.UserId.Desc);
            query.PageIndex = pageIndex;
            query.PageSize = 5;
            query.ReturnMatchedCount = true;
            var users = new Users();
            users = ServiceManager.UserService.GetUsers(query);

            #endregion

            ViewBag.PageIndex = pageIndex;
            ViewBag.TotalCount = users.TotalCount;
            ViewBag.Keyword = kw;
            return View(users);
        }


        #region 管理员：删除
        public ActionResult Delete(int id, int page,string kw)
        {
            if (id < 1)
            {
                throw new Exception("删除操作失败，ID为0");
            }
            var rs =false;
            //若删除失败，需要提示
            if (!rs)
            {
                return Content("<script>alert('管理员删除失败');location.href='" + Url.Action("Index", (new { page = page,kw = kw})) + "'</script>", "text/html");
            }
            return RedirectToAction("Index", new { page = page,kw = kw});
        }

        #endregion

        #region 管理员：详情页

        public ActionResult Detail(int id)
        {
            if (id == 0)
            {
                throw new Exception("加载出错，Id为0.");
            }
            var userInfo = ServiceManager.UserService.GetUser(id);
            return View(userInfo);
        }

        #endregion
    }
}
